package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.Device;
import com.android.equipment.dao.DeviceModel;
import com.android.equipment.dao.Device_;
import com.android.equipment.dao.People;
import com.android.equipment.dao.People_;

import java.util.List;
import java.util.Objects;

import io.objectbox.Box;
import io.objectbox.query.Query;

public class DaoDevice {

    static Box<Device> deviceBox;
    private static DaoDevice instance;

    private DaoDevice() {
    }

    public static DaoDevice getInstance() {
        if (instance == null) {
            instance = new DaoDevice();
            deviceBox = ObjectBox.get().boxFor(Device.class);
        }
        return instance;
    }

    public void addDevice(Device device) {
        deviceBox.put(device);
    }

    public void deleteDevice(Device device) {
        deviceBox.remove(device);
    }

    public void updateDevice(Device device) {
        deviceBox.put(device);
    }

    public Device getDevice(long id) {
        return deviceBox.get(id);
    }

    public List<Device> getAllDevice(){
        return deviceBox.getAll();
    }

    public List<Device> getNotDelDevice() {
        Query<Device> query = deviceBox.query(Device_.isDel.equal(false)).build();
        List<Device> deviceList = query.find();
        query.close();
        return deviceList;
    }

    public List<Device> getDelDevice() {
        Query<Device> query = deviceBox.query(Device_.isDel.equal(true)).build();
        List<Device> deviceList = query.find();
        query.close();
        return deviceList;
    }

    public long getDeviceModelId(String deviceCode) {
        return Objects.requireNonNull(deviceBox.query(Device_.deviceCode.equal(deviceCode)).build().findUnique()).getDeviceModelId();
    }

    public Device getDeviceByDeviceCode(String deviceCode){
        return deviceBox.query(Device_.deviceCode.equal(deviceCode)).build().findUnique();
    }
}
